GBML manual / Maths

wave

A value that rises and falls between min and max and back, forever, like a slow pulse - a torch's glow, a pickup floating up and down, a menu highlight breathing in and out.

Syntax

wave(min, max, duration, offset) -> real

Arguments

NameType
minreal
maxreal
durationreal
offsetreal

Returns

real

Description

A value that rises and falls between min and max and back, forever, like a slow pulse - a torch's glow, a pickup floating up and down, a menu highlight breathing in and out. Built on game_frame, so nothing calling this ever needs its own timer variable.

Example

y = ystart + wave(-4, 4, 40, 0);

Floats this instance up and down 4 pixels either side of its starting y, one full up-and-down cycle every 40 frames - a plain, no-state-of-its-own idle animation for a pickup or a floating platform.

duration is in frames, not seconds

A full rise-and-fall cycle takes duration frames - wave(0, 10, 60, 0) breathes once a second at the GBA's own ~60 frames-per-second. This matches every other time-shaped thing in GBML (alarm, frame_speed, path_speed are all frame-based too), so if you think in seconds, convert once at the call site: wave(0, 10, seconds * 60, 0).

offset shifts the phase

offset is a fraction of one cycle to shift the start of this call by - 0 is no shift, 0.5 starts exactly half a cycle ahead. This is how two torches (or two calls sharing the same duration) pulse out of sync with each other, without either one owning a separate clock:

glow = wave(60, 100, 90, 0);       // this torch
// a second torch, same duration, opposite phase:
glow2 = wave(60, 100, 90, 0.5);

Cost

Reads game_frame() internally, an always-advancing, project-wide frame counter since boot - see its own page for exactly what "always advances" means (through a pause, through a room change) and why. min/max may arrive in either order, swapped first like clamp. duration of 0 or less has no cycle to be partway through, and returns min rather than dividing by zero. One sine lookup (angle_sin) plus a handful of multiply/divides - cheap enough to call every frame for several instances at once.

On the GBA

duration IS IN FRAMES, not seconds - one full rise-and-fall cycle takes duration frames (wave(0, 10, 60, 0) breathes once a second at the GBA's own ~60fps). Reads game_frame() internally, an always-advancing, project-wide frame counter since boot (see its own page) - a wave never needs its own timer variable, and two wave() calls sharing the same duration stay in lockstep automatically. offset shifts the phase by that fraction of one cycle (0.5 starts exactly half a cycle ahead) - the way to make two things pulse out of sync without either owning a separate clock. min/max may arrive either order, swapped first like clamp. duration of 0 or less has no cycle to be partway through and returns min rather than dividing by zero.

See also: game_framewrapangle_sin